//
//  SCNDataSourceMiddle.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/4/26.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNDataSourceMiddle: NSObject {
    
    typealias didSelectCell = (Int) -> Void
    
    var data: [Any]?
    
    var identifier: String? = "default"
    
    var selectCell: didSelectCell?
    
    
    
    
    init(_ obj: Array<Any>,iden: String) {
        data = obj as? [NSObject]
        identifier = iden
        
    }
    
}

extension SCNDataSourceMiddle: UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard let tmp = data else {
            return 0
        }
        return tmp.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let iden = identifier else {
            return UITableViewCell()
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: iden, for: indexPath)
        
        cell.textLabel?.text = data?[indexPath.row] as? String
        
        return cell
    }
    
    
 
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        defer {
            tableView.deselectRow(at: indexPath, animated: true)
        }
        if let action = selectCell {
            printLog(data?[indexPath.row])
            action(indexPath.row)
        }
        
    }
}
